﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.User;
using CE.Global;
using CE.Configuration;
using CE.Provider;

public partial class Provider_CarInsurance_ProviderCarMakeModel : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCICMDL";
    protected void Page_Load(object sender, EventArgs e)
    {
        //usrLink.PageName = "Provider Car Make Model";

        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {
            GetCXCarMake();
            GetProviderCarMake();

            usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2;

            if (Request.Params["CarMakeId"] != null && Request.Params["CarModelId"] != null)
            {
                int _carmakeid = 0;
                int _carmodelid = 0;
                if (int.TryParse(Request.Params["CarMakeId"].ToString(), out _carmakeid) &&
                    int.TryParse(Request.Params["CarModelId"].ToString(), out _carmodelid))
                {
                    ddlProviderCarMake.Text = _carmakeid.ToString();
                    ddlProviderCarMake_SelectedIndexChanged(null, null);
                    ddlCXCarModel.Text = _carmodelid.ToString();
                    GetCarMakeModelDetails(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),_carmakeid, _carmodelid);
                }
            }

            if (Session[CE.Global.General.ProviderCompanyId].ToString() == CE.Configuration.MyConfigurationManager.GetAppSettingValue("HSBCProviderCompanyId"))
            {
                lblStatus.Text = "Accept";
            }
            else
            {
                lblStatus.Text = "Status";
            }
        }

    }

    //private void GetCXCarMake()
    //{
    //    CarMakeDB _carmakedb = new CarMakeDB();
    //    ddlCXCarMake.DataTextField = "Description";
    //    ddlCXCarMake.DataValueField = "CarMakeId";
    //    ddlCXCarMake.DataSource = _carmakedb.GetCarMakeList();
    //    ddlCXCarMake.DataBind();

    //    ddlCXCarMake.Items.Insert(0, new ListItem("Select", "-1"));
 
    //}

    private void GetCXCarModelDetail(Int32 CarMakeId, Int32 CarModelId)
    {
        CE.CarInsurance.CarModelDB _carmodeldb = new CE.CarInsurance.CarModelDB();
        CE.CarInsurance.CarModel _carmodel = new CE.CarInsurance.CarModel();
        _carmodel = _carmodeldb.GetCarModel(CarMakeId, CarModelId);
        if (_carmodel != null)
        {
            lblBody.Text = _carmodel.Body;
            lblCarCode.Text = _carmodel.CarCode;
            lblCC.Text = _carmodel.CC;
            txtCC.Text = _carmodel.CC;
        }
    }

    protected void ddlCXCarMake_SelectedIndexChanged(object sender, EventArgs e)
    {
        /*ClearFields();
        GetCXCarModel();

        //Get Provider Car Make Match
        CarMakeDB _providercarmakedb = new CarMakeDB();
        CarMake _providercarmake = new CarMake();
        _providercarmake = _providercarmakedb.GetCarMakeItemByCarMakeId(Convert.ToInt32(ddlCXCarMake.SelectedValue), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));

        if (_providercarmake != null)
            ddlCarMake.SelectedValue = _providercarmake.CarMakeId.ToString();
        else
            ddlCarMake.SelectedIndex = 0;

        GetCarModel();*/
    }

    protected void ddlCXCarModel_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Get Provider Car Model Match
        //CarMakeModel _providercarmakemodel = new CarMakeModel();
        //CarMakeModelDB _providercarmakemodeldb = new CarMakeModelDB();

        ClearFields();
        GetCXCarModelDetail(Convert.ToInt32(ddlProviderCarMake.Text), Convert.ToInt32(ddlCXCarModel.Text));
        //_providercarmakemodel = _providercarmodeldb.GetProviderCarMakeModelItem(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlProviderCarMake.Text), Convert.ToInt32(ddlCXCarModel.Text));
        GetCarMakeModelDetails(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlProviderCarMake.Text), Convert.ToInt32(ddlCXCarModel.Text));
        //if ((_providercarmodel != null) && (ddlCarModel.Items.Count > 0))
        //    ddlcxCarModel.SelectedValue = _providercarmodel.CarModelId.ToString();
        //else
        //    ddlCarModel.SelectedIndex = 0;
    }
  
    private void GetProviderCarMake()
    {
        CE.Provider.CarMakeList _carmakelist = new CE.Provider.CarMakeList();
        CE.Provider.CarMakeDB _carmakedb = new CE.Provider.CarMakeDB();

        _carmakelist = _carmakedb.GetProviderMakeList(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()));
        if (_carmakelist != null)
        {
            for (int i = 0; i < _carmakelist.Count; i++)
            {
                ddlProviderCarMake.Items.Add(new ListItem(_carmakelist[i].Description, _carmakelist[i].CarMakeId.ToString()));
            }
        }
        //CarMakeDB _providercarmakedb = new CarMakeDB();
        //DataTable dtCarMake = _providercarmakedb.GetCarMakeDT(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));
        //ddlCarMake.DataSource = dtCarMake;
        //ddlCarMake.DataTextField = "Description";
        //ddlCarMake.DataValueField = "CarMakeId";        
        //ddlCarMake.DataBind();

        ddlProviderCarMake.Items.Insert(0, new ListItem("Select", "-1"));
    }

    private void GetCXCarMake()
    {
        CE.CarInsurance.CarMakeList _cxcarmakelist = new CE.CarInsurance.CarMakeList();
        CE.CarInsurance.CarMakeDB _cxcarmakedb = new CE.CarInsurance.CarMakeDB();

        _cxcarmakelist = _cxcarmakedb.GetCarMakeList();
        if (_cxcarmakelist != null)
        {
            for (int i = 0; i < _cxcarmakelist.Count; i++)
            {
                ddlCXCarMake.Items.Add(new ListItem(_cxcarmakelist[i].Description, _cxcarmakelist[i].CarMakeId.ToString()));
            }
        }
        ddlCXCarMake.Items.Insert(0, new ListItem("Select", "-1"));
    }

    private void GetCXCarModel(Int32 CarMakeId)
    {
        //CarModelDB _providercarModeldb = new CarModelDB();
        //DataTable dtCarModel = _providercarModeldb.GetCarModelDT(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));       
        ddlCXCarModel.Items.Clear();
        CE.CarInsurance.CarModelList _carmodellist = new CE.CarInsurance.CarModelList();
        CE.CarInsurance.CarModelDB _carmodeldb = new CE.CarInsurance.CarModelDB();
        _carmodellist = _carmodeldb.GetCarModelList(CarMakeId);

        if (_carmodellist != null)
        {
            for (int i = 0; i < _carmodellist.Count; i++)
            {
                ddlCXCarModel.Items.Add(new ListItem(_carmodellist[i].Description, _carmodellist[i].CarModelId.ToString()));
            }
        }
        //CarMakeModelDB _providercarmakedb = new CarMakeModelDB();
        //DataTable dtCarModel = _providercarmakedb.GetCarMakeModelDT(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCarMake.SelectedValue));
        //ddlCarModel.DataSource = dtCarModel;
        //ddlCarModel.DataTextField = "Description";
        //ddlCarModel.DataValueField = "CarModelId";
        //ddlCarModel.DataBind();

        ddlCXCarModel.Items.Insert(0, new ListItem("Select", "-1"));

    }

    /*private void GetCarModel()
    {
        //CarModelDB _providercarModeldb = new CarModelDB();
        //DataTable dtCarModel = _providercarModeldb.GetCarModelDT(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));       

        CarMakeModelDB _providercarmakedb = new CarMakeModelDB();
        DataTable dtCarModel = _providercarmakedb.GetCarMakeModelDT(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCarMake.SelectedValue));
        ddlCarModel.DataSource = dtCarModel;
        ddlCarModel.DataTextField = "Description";
        ddlCarModel.DataValueField = "CarModelId";
        ddlCarModel.DataBind();

        ddlCarModel.Items.Insert(0, new ListItem("Select", "-1"));
        
    }*/

    private void GetCarMakeModelDetails(Int64 ProviderCompanyId, Int32 CarMakeId, Int32 CarModelId)
    {
        CarMakeModel _providercarmakemodel = new CarMakeModel();
        CarMakeModelDB _providercarmakemodeldb = new CarMakeModelDB();

        _providercarmakemodel = _providercarmakemodeldb.GetProviderCarMakeModelItem(ProviderCompanyId, CarMakeId, CarModelId);
      
        if (_providercarmakemodel != null)
        {
            //txtCarCode.Text = _providercarmakemodel.CarCode;
            txtProviderCarModel.Text = _providercarmakemodel.Description;
            lblCarCode.Text = _providercarmakemodel.CarCode;            
            ddlBody.SelectedValue = _providercarmakemodel.Body;
            txtNoOfDoors.Text = _providercarmakemodel.NumberOfDoors == 0 ? "" : _providercarmakemodel.NumberOfDoors.ToString();
            txtCC.Text = _providercarmakemodel.CC == 0 ? "" : _providercarmakemodel.CC.ToString();
            rdoTurbo.Text = _providercarmakemodel.TurboEngine.ToString();
            rdoPImport.Text = _providercarmakemodel.ParallelImport.ToString();
            txtRate.Text = _providercarmakemodel.Rate==0 ? "" : _providercarmakemodel.Rate.ToString("#.00");
            txtAgent.Text = _providercarmakemodel.Agent ==0 ? "" : _providercarmakemodel.Agent.ToString();
            txtUW.Text = _providercarmakemodel.UW ==0 ? "" : _providercarmakemodel.UW.ToString();
            rdoRefer.Text = _providercarmakemodel.IsRefer.ToString();
            chkStatus.Checked = _providercarmakemodel.Status;
            
            //get other details
            lblBody.Text = _providercarmakemodel.Body2;
            lblCC.Text = _providercarmakemodel.CC2.ToString();

            if (_providercarmakemodel.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _providercarmakemodel.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }

            usrEffectivityDate.Day = _providercarmakemodel.DInfo1.Day;
            usrEffectivityDate.Month = _providercarmakemodel.DInfo1.Month;
            usrEffectivityDate.Year = _providercarmakemodel.DInfo1.Year;
            
 
        }
    }

    private Int64 CheckCarMakeModelDetails()
    {
        CarMakeModel _providercarmakemodel = new CarMakeModel();
        CarMakeModelDB _providercarmakemodeldb = new CarMakeModelDB();
        Int64 _retValue = 0;
        _providercarmakemodel = _providercarmakemodeldb.GetProviderCarMakeModelItem(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlProviderCarMake.Text), Convert.ToInt32(ddlCXCarModel.Text));

        if (_providercarmakemodel != null)
        {
            _retValue = _providercarmakemodel.ProviderCarMakeModelId;
        }

        return _retValue;
    }

    //protected void ddlCarMake_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    ClearFields();
    //    GetCarModel();
    //    ddlCXCarMake.SelectedValue = ddlCarMake.SelectedValue; 
    //    GetCXCarModel();
    //    GetCarMakeModelDetails();
    //}

    //protected void ddlCarModel_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    ClearFields();
    //    ddlCXCarModel.SelectedValue = ddlCarModel.SelectedValue; 
    //    GetCarMakeModelDetails();        
    //}

    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);

            if (CheckAuthCode(_authcode, 100))
            {
                CarMakeModel _providercarmakemodel = new CarMakeModel();
                CarMakeModelDB _providercarmakemodeldb = new CarMakeModelDB();

                int _trantype = 1;

                _providercarmakemodel.ProviderCarMakeModelId = CheckCarMakeModelDetails();
                if (_providercarmakemodel.ProviderCarMakeModelId > 0)
                {
                    _trantype = 2;
                }

                _providercarmakemodel.CarMakeId = Convert.ToInt32(ddlCXCarMake.Text);
                _providercarmakemodel.CarModelId = Convert.ToInt32(ddlCXCarModel.Text);
                _providercarmakemodel.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
                _providercarmakemodel.Description = txtProviderCarModel.Text;
                _providercarmakemodel.CarCode = lblCarCode.Text;
                _providercarmakemodel.Body = ddlBody.SelectedValue;
                _providercarmakemodel.NumberOfDoors = Convert.ToInt32(txtNoOfDoors.Text);
                _providercarmakemodel.CC = Convert.ToInt32(txtCC.Text);
                _providercarmakemodel.TurboEngine = Convert.ToBoolean(rdoTurbo.Text);
                _providercarmakemodel.ParallelImport = Convert.ToBoolean(rdoPImport.Text);
                _providercarmakemodel.Rate = float.Parse(txtRate.Text);
                _providercarmakemodel.Agent = Convert.ToInt32(txtAgent.Text);
                _providercarmakemodel.UW = Convert.ToInt32(txtUW.Text); ;
                _providercarmakemodel.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                _providercarmakemodel.IsRefer = Convert.ToBoolean(rdoRefer.Text);
                _providercarmakemodel.Status = chkStatus.Checked;

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _providercarmakemodel.FInfo1 = float.Parse(txtNewRate.Text);
                    _providercarmakemodel.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _providercarmakemodel.FInfo1 = float.Parse(txtRate.Text);
                    _providercarmakemodel.DInfo1 = CE.Global.General.GetUTCDateTime();

                  
                }

                _providercarmakemodel.ProviderCarMakeModelId = _providercarmakemodeldb.Insert(_providercarmakemodel);
                if (_providercarmakemodel.ProviderCarMakeModelId > 0)
                {
                    InsertProviderAuthTransaction(_providercarmakemodel.ProviderCarMakeModelId, "CICarMakeModel", _trantype, 100, _authcode, _providercarmakemodel.ProviderCompanyId, _providercarmakemodel.RefCEUserId);
                    ProviderUpdateSave(_providercarmakemodel.ProviderCarMakeModelId);
                    lblMessage.Text = "Updated successfully!";
                    Response.Redirect("ViewProviderCarMakeModel.aspx");
                }
                else
                {
                    lblMessage.Text = "Updated failed.";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarMakeModel";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        rfvAuthCode.Validate();
        if (CheckCarMakeModelDetails() != 0 && txtAuthCode.Text.Trim().Length > 0)
        {
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
            if (CheckAuthCode(_authcode, 100))
            {
                if (CheckCarMakeModelDetails() != 0)
                {
                    CarMakeModelDB _providercarmakemodeldb = new CarMakeModelDB();
                    if (_providercarmakemodeldb.Delete(CheckCarMakeModelDetails(), _providercompanyid))
                    {
                        InsertProviderAuthTransaction(CheckCarMakeModelDetails(), "CICarMakeModel", 3, 100, _authcode, _providercompanyid, _refceuserid);
                        Response.Redirect("ViewProviderCarMakeModel.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }

    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarMakeModel.aspx");
    }

    private void ClearFields()
    {
        //txtCarCode.Text = _providercarmakemodel.CarCode;
        txtProviderCarModel.Text = "";
        lblCarCode.Text = "";
        ddlBody.Text = "-1";
        txtNoOfDoors.Text = "";
        txtCC.Text = "";
        rdoTurbo.Text = "True";
        rdoPImport.Text = "True";
        txtRate.Text = "";
        txtAgent.Text = "";
        txtUW.Text = "";
        rdoRefer.Text = "True";


        //get other details
        lblBody.Text = "";
        lblCC.Text = "";
        
    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }

    protected void ddlProviderCarMake_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        
        ddlCXCarMake.Text = ddlProviderCarMake.Text;
        GetCXCarModel(Convert.ToInt32(ddlProviderCarMake.Text));
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        ClearFields();
        ddlProviderCarMake.Text = "-1";
        ddlProviderCarMake_SelectedIndexChanged(null, null);
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            btnSave.Enabled = false;
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            btnSave.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnSave.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
